<?php
include ('chek.php');
//修改标题或内容
$pid=$input->get('pid');
if($pid>0){
    $sql="select * from page WHERE pid='{$pid}' ";
    $res=$db->query($sql);
    $buser=$res->fetch_array(MYSQLI_ASSOC);
}
if(!$pid){
    $buser=array(
        'title'=>'',
        'aptor'=>'',
        'contact'=>''
    );
}

//添加内容
if($input->get('do')=='add') {
    $title = $input->post('title');
    $aptor = $input->post('aptor');
    $content = $input->post('content');
    if (empty($title) or empty($aptor) || empty($content)) {
        die('数据不能为空');
    }
    if($pid>0){
        $uptim=time();
        $sql="UPDATE page SET title='{$title}',aptor='{$aptor}',contact='{$content}',uptime='{$uptim}' WHERE pid='{$pid}'";
    }else{
        $tim=time();
        $sql="insert into page(title,contact,aptor,intime,uptime) VALUES ('{$title}','{$content}','{$aptor}','{$tim}','0')";
    }
    $mysql_result=$db->query($sql);
    if($mysql_result){
        header('location:blog.php');
    }else{
        die('执行失败');
    }
}
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8"/>
    <title>添加博客</title>
    <?php include (PATH.'/header.inc.php'); ?>
    <!-- kindEditor 编辑器代码 -->
    <link rel="stylesheet" href="../public/kindEditor/themes/default/default.css" />
    <script charset="utf-8" src="../public/kindEditor/kindeditor-min.js"></script>
    <script charset="utf-8" src="../public/kindEditor/lang/zh_CN.js"></script>
    <script type="text/javascript">
        KindEditor.ready(function(K) {
            window.editor = K.create('#content',{
                cssPath:'../public/kindEditor/plugins/code/prettify.css',
                uploadJson:'../public/kindEditor/php/upload_json.php',
                fileManagerJson:'../public/kindEditor/php/file_manager_json.php',
                resizeType :1,
                minHeight:250,
                allowPreviewEmoticons : true,
                allowImageUpload : true
            });
        });
    </script>
</head>
<body>
<?php include ('./nav.inc.php')?>
<div class="container">
    <div class="row">
        <h1>博客添加<small class="pull-right"><a class="btn btn-default" href="blog.php">返回</a></small></h1>
        <hr/>
        <form class="form-horizontal" action="blog_add.php?do=add&&pid=<?php echo $pid?>" method="post">
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">标题</label>
                <div class="col-sm-6">
                    <input type="text" class="form-control" name="title" placeholder="请输入标题" value="<?php echo $buser['title'] ?>">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">作者</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="aptor" placeholder="请输入作者" value="<?php echo $buser['aptor'] ?>" >
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">内容</label>
                <div class="col-sm-8">
                    <textarea id="content"  name="content" class="form-control" placeholder="请输入正文" ><?php echo $buser['contact']?></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-6">
                    <button type="submit" class="btn btn-default"> 提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
</html>